package com.spr.utils;

import org.springframework.beans.BeanUtils;
import org.springframework.util.CollectionUtils;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

public class BeansUtils {
    /**
     *
     * @param list
     * @param tClass 必须要有无参构造方法
     * @param <T>
     * @param <P>
     * @return
     */
    public  static  <T,P> List<T> copyList(List<P> list, Class<T> tClass ) {
            if (CollectionUtils.isEmpty(list)){
                return null;
            }
            List<T> tList =new ArrayList<>();
        try {
            Constructor<T> constructor = tClass.getConstructor();
            for (P p : list) {
                T t1 = constructor.newInstance();
                BeanUtils.copyProperties(p,t1);
                tList.add(t1);
            }
            return tList;
        }
        catch (NoSuchMethodException | IllegalAccessException | InstantiationException | InvocationTargetException e) {
            e.printStackTrace();
            return null;
        }

        }







}
